package com.zzhrtech.wgzx_cityzen.adapters.contact;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.squareup.picasso.Picasso;
import com.zzhrtech.WgzxApplication;
import com.zzhrtech.wgzx_cityzen.R;
import com.zzhrtech.wgzx_cityzen.base.OnItemClickLitener;
import com.zzhrtech.wgzx_cityzen.beans.contact.ActivListBean;
import com.zzhrtech.wgzx_cityzen.beans.contact.JifenGoodsBean;
import com.zzhrtech.wgzx_cityzen.ui.contact.GoodsActivity;
import com.zzhrtech.wgzx_cityzen.utils.DensityUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by renxiangpeng on 2017/2/23.
 */

public class GoodsRecyclerViewAdapter extends RecyclerView.Adapter<GoodsRecyclerViewAdapter.NormalTextViewHolder> {
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private List<JifenGoodsBean.Data> beanList;
    private OnItemClickLitener mOnItemClickLitener;

    public GoodsRecyclerViewAdapter(Context context) {
        mContext = context;
        beanList = new ArrayList<JifenGoodsBean.Data>();
        mLayoutInflater = LayoutInflater.from(context);
    }

    @Override
    public NormalTextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NormalTextViewHolder(mLayoutInflater.inflate(R.layout.item_jifengrid, parent, false));
    }

    @Override
    public void onBindViewHolder(NormalTextViewHolder holder, int position) {
        holder.tv_name.setText(beanList.get(position).getGood_name());
        holder.tv_num.setText(beanList.get(position).getGood_num() + "积分");

        Log.d("here===========","asd");

        //type 1 为自取，其他为换码
        //codenum 0 为缺码， 其他有
        //inventory 库存
        if (beanList.get(position).getGood_type().equals("1")){
            if (beanList.get(position).getInventory() == 0 ){
                holder.tv_mengview.setVisibility(View.VISIBLE);
            }else {
                holder.tv_mengview.setVisibility(View.GONE);
            }
        }else {
            if (beanList.get(position).getCodenum().equals("0")){
                holder.tv_mengview.setVisibility(View.VISIBLE);
            }else {
                holder.tv_mengview.setVisibility(View.GONE);
            }
        }



        if (((GoodsActivity) mContext).islist) {
            Glide.with(mContext)
                    .load(beanList.get(position).getGood_imgth())
                    .override(WgzxApplication.getInstance().getwidth() - DensityUtil.dip2px(mContext, 16), (WgzxApplication.getInstance().getwidth() - DensityUtil.dip2px(mContext, 16)) * 348 / 690)
                    .centerCrop()
                    .placeholder(R.mipmap.ic_placehoder)
                    .into(holder.iv_type);
        }else {
            Glide.with(mContext)
                    .load(beanList.get(position).getGood_imgth())
                    .override(WgzxApplication.getInstance().getwidth() / 2 - DensityUtil.dip2px(mContext, 16), (WgzxApplication.getInstance().getwidth() / 2 - DensityUtil.dip2px(mContext, 16)) * 348 / 690)
                    .centerCrop()
                    .placeholder(R.mipmap.ic_placehoder)
                    .into(holder.iv_type);
        }

        holder.position = position;
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    //添加列表
    public void addItem(JifenGoodsBean.Data bean){
        beanList.add(bean);
        notifyDataSetChanged();
    }
    //清空列表
    public void cleanList(){
        beanList.clear();
    }

    public int getGoodsInen(int position){
        return beanList.get(position).getInventory();
    }

    //获得id
    public String getGoodsId(int position){
        return beanList.get(position).getGood_id();
    }

    public class NormalTextViewHolder extends RecyclerView.ViewHolder {
        TextView tv_num;
        ImageView iv_type;
        TextView tv_mengview;
        TextView tv_name;
        int position;
        NormalTextViewHolder(View view) {
            super(view);
            tv_num = (TextView) view.findViewById(R.id.tv_num);
            iv_type = (ImageView) view.findViewById(R.id.iv_type);
            tv_mengview = (TextView) view.findViewById(R.id.tv_mengview);
            tv_name = (TextView) view.findViewById(R.id.tv_name);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickLitener != null){
                        mOnItemClickLitener.onItemClick(v,position);
                    }
                }
            });
        }
    }


    public void setOnItemClickLitener(OnItemClickLitener mOnItemClickLitener)
    {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}